package com.jzo2o.foundations.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.foundations.enums.FoundationHotEnum;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务表 服务实现类
 * </p>
 *
 * @author lz
 * @since 2025-10-28
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {

    @Autowired
    private ServeItemServiceImpl serveItemService;
    @Autowired
    private RegionServiceImpl regionService;

    /**
     * 分页查询
     *
     * @return 分页结果
     */


    /**
     * 分页查询服务列表
     * @param servePageQueryReqDTO 查询条件
     * @return 分页列表
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
        //调用mapper查询数据，这里由于继承了ServiceImpl<ServeMapper, Serve>，使用baseMapper相当于使用ServeMapper
        PageResult<ServeResDTO> serveResDTOPageResult = PageHelperUtils.selectPage(servePageQueryReqDTO, () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
        return serveResDTOPageResult;
    }

    /**
     * 批量新增
     * @param serveUpsertReqDTOList 批量新增数据
     */
    @Override
    public void batchAdd(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        for (ServeUpsertReqDTO dto : serveUpsertReqDTOList) {
            //判断服务项是否存在
            ServeItem item = serveItemService.getById(dto.getServeItemId());
            if (ObjectUtils.isEmpty( item)) {
                throw new ForbiddenOperationException("服务项不存在");
            }
            //判断服务项是否启动
            if (item.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
                throw new ForbiddenOperationException("服务项未启动,不能添加");
            }
            //判断服务项是否已经添加
            Integer count =this.lambdaQuery().eq(Serve::getServeItemId, dto.getServeItemId())
                    .eq(Serve::getRegionId, dto.getRegionId())
                    .count();
            if (count > 0) {
                throw new ForbiddenOperationException("服务项已添加,不能重复添加");
            }
            //查询区域信息
            String cityCode = regionService.getById(dto.getRegionId()).getCityCode();
            //复制服务项为服务信息
            Serve serve = BeanUtils.copyProperties(dto, Serve.class);
            serve.setCityCode(cityCode);
            //新增区域服务信息
            boolean save = this.save(serve);
            if (!save) {
                throw new ForbiddenOperationException("服务添加失败");
            }
        }
    }

    /**
     * 修改服务价格
     * @param id
     * @param price
     */
    @Override
    public void updatePrice(Long id, BigDecimal price) {
        //判断服务是否存在
        Serve serve = this.getById(id);
        if (ObjectUtils.isEmpty(serve)) {
            throw new ForbiddenOperationException("服务不存在");
        }
        boolean update = this.lambdaUpdate().set(Serve::getPrice, price)
                .eq(Serve::getId, id)
                .update();
        if (!update) {
            throw new ForbiddenOperationException("服务价格修改失败");
        }
    }

    /**
     * 上架
     * @param id
     */
    @Transactional
    @Override
    public void onSale(Long id) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);
        if(ObjectUtils.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在，禁止上架");
        }

        //2.判断是否有区域
        if(serve.getRegionId()==null) {
            throw new ForbiddenOperationException("区域未选，禁止上架");
        }

        //3.判断服务是否上架过
        if(serve.getSaleStatus()==FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("不要重复上架");
        }

        //4.判断服务项是否存在
        ServeItem item = serveItemService.getById(serve.getServeItemId());
        if(ObjectUtils.isEmpty(item)){
            throw new ForbiddenOperationException("服务项不存在，禁止上架");
        }

        //5.判断服务项是否已启动
        if(item.getActiveStatus()!=FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务项未启用，禁止上架");
        }

        //5.修改状态为已上架
        boolean result = this.lambdaUpdate().set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus()).eq(Serve::getId, id).update();
        if(!result) {
            throw new DBException("服务上架失败");
        }
    }

    @Transactional
    @Override
    public void offSale(Long id) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);
        if(ObjectUtils.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在，禁止下架");
        }

        //2.判断是否有区域
        if(serve.getRegionId()==null) {
            throw new ForbiddenOperationException("区域未选，禁止下架");
        }

        //3.判断服务是否上架过
        if(serve.getSaleStatus()==FoundationStatusEnum.DISABLE.getStatus()) {
            throw new ForbiddenOperationException("不要重复下架");
        }

        //4.判断服务项是否存在
        ServeItem item = serveItemService.getById(serve.getServeItemId());
        if(ObjectUtils.isEmpty(item)){
            throw new ForbiddenOperationException("服务项不存在，禁止下架");
        }

        //5.修改状态为已上架
        boolean result = this.lambdaUpdate().set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus()).eq(Serve::getId, id).update();
        if(!result) {
            throw new DBException("服务下架失败");
        }
    }

    @Override
    public void serve(Long id) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);
        if(ObjectUtils.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在，删除失败");
        }
        //2.判断服务是否是草稿
        if(serve.getSaleStatus()!=FoundationStatusEnum.INIT.getStatus()) {
            throw new ForbiddenOperationException("服务不是草稿，删除失败");
        }
        boolean result = this.removeById(id);
        if(!result) {
            throw new DBException("服务删除失败");
        }
    }

    @Override
    public void onHot(Long id) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);
        if(ObjectUtils.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在，禁止设置热门");
        }

        //2.判断服务是否已上架
        if(serve.getSaleStatus()!=FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务未上架，禁止设置热门");
        }
        //3.判断服务是否已设为热门
        if(serve.getIsHot() == FoundationHotEnum.DISABLE.getStatus()) {
            throw new ForbiddenOperationException("服务已设为热门，禁止重复设置");
        }
        boolean result = this.lambdaUpdate().set(Serve::getIsHot, FoundationHotEnum.DISABLE.getStatus()).eq(Serve::getId, id).update();
        if(!result) {
            throw new DBException("服务设置热门失败");
        }
    }

    @Override
    public void offHot(Long id) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);
        if(ObjectUtils.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在，禁止取消热门");
        }

        //2.判断服务是否已上架
        if(serve.getSaleStatus()!=FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务未上架，禁止取消热门");
        }
        //3.判断服务是否已设为热门
        if( serve.getIsHot() == FoundationHotEnum.INIT.getStatus()){
            throw new ForbiddenOperationException("服务未设为热门，禁止取消热门");
        }
        boolean result = this.lambdaUpdate().set(Serve::getIsHot, FoundationHotEnum.INIT.getStatus()).eq(Serve::getId, id).update();
        if(!result) {
            throw new DBException("服务取消热门失败");
        }
    }
}
